package com.hsmpay.utils;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import com.hsmpay.qian4.pojo.df.DFContentResult;

import java.io.ByteArrayInputStream;
import java.util.*;


public class XmlUtil {
	public Document xmlDoc=null;
	public XmlUtil(String xmlStr){
		try {
		SAXReader reader = new SAXReader();
		ByteArrayInputStream bis = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));
		xmlDoc = reader.read(bis);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public XmlUtil(String xmlStr,String charset){
		try {
		SAXReader reader = new SAXReader();
		ByteArrayInputStream bis = new ByteArrayInputStream(xmlStr.getBytes(charset));
	
		xmlDoc = reader.read(bis);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getNodeText(String xpath){
		String result=""; 		
		try {
			Node nameNode = xmlDoc.selectSingleNode(xpath);
			if (nameNode!=null)
				result=nameNode.getText().trim();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
   
//	@SuppressWarnings("unchecked")
//	public String getNodeList(String xpath,String childPaths){
//    	String result="";
//		List<Node> list = xmlDoc.selectNodes(xpath);
//		String[] childPath=childPaths.split("\\|");
//		int c = 0;
//	    for(Iterator i = list.iterator(); i.hasNext(); )
//	    {
//  	        Element e = (Element) i.next();
//  	        if(c != 0) result+="|";
//			for(int j=0;j<childPath.length;j++)
//			{
//				String[] child= childPath[j].split("\\,");
//				String nodeText=e.element(child[1]).getText();
//				System.out.println(child[1]+":"+nodeText+"["+child[0]+"]");
//				if(j != 0)result+=",";
//				result+= nodeText;
//  	        }
//			System.out.println("");
//			c++;
//  	    }
//		return result;
//	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getNodeList(String xpath,String childPaths){
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		Map<String,Object> resultMap = null;
		List<Node> list = xmlDoc.selectNodes(xpath);
		String[] childPath=childPaths.split("\\|");
	    for(Iterator i = list.iterator(); i.hasNext(); ){
  	        Element e = (Element) i.next();
  	        resultMap = new HashMap<String, Object>();
			for(int j=0;j<childPath.length;j++)
			{
				String[] child= childPath[j].split("\\,");
				//System.out.println(child[1]);
				String nodeText="";
				if( null != e.element(child[1]))
					nodeText=e.element(child[1]).getText();
				//System.out.println(child[1]+":"+nodeText+"["+child[0]+"]");
				if(child.length > 2)
					resultMap.put(child[2], nodeText);
				else
					resultMap.put(child[1], nodeText);
  	        }
			result.add(resultMap);
  	    }
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public Map<String,Object> getNodeListByXingye(String xpath){
		Map<String,Object> result = new HashMap<String,Object>();
		Map<String,Object> resultMap = null;
		List<Node> list = xmlDoc.selectNodes(xpath);
	    for(Iterator i = list.iterator(); i.hasNext(); ){
	    	Element e = (Element) i.next();
//	    	String SRVRTID = getText(e.element("SRVRTID"));//交易序号
//	    	String TRNTYPE = getText(e.element("TRNTYPE"));//交易类型
//	    	String TRNCODE = getText(e.element("TRNCODE"));//核心摘要代码
	    	String DTACCT = getText(e.element("DTACCT"));//记账日期
	    	String TRNAMT = getText(e.element("TRNAMT"));//交易金额
//	    	String BALAMT = getText(e.element("BALAMT"));//余额
//	    	String CURRENCY = getText(e.element("CURRENCY"));//币种
//	    	String MEMO = getText(e.element("MEMO"));//用途
	    	String CORRELATE_ACCTID = getText(e.element("CORRELATE_ACCTID"));//对方账号
	    	String CORRELATE_NAME = getText(e.element("CORRELATE_NAME"));//对方账户名称
//	    	String CHEQUENUM = getText(e.element("CHEQUENUM"));//本行凭证代号
//	    	String BILLTYPE = getText(e.element("BILLTYPE"));//他行凭证类型
//	    	String BILLNUMBER = getText(e.element("BILLTYPE"));//他行凭证号码
	    	String CORRELATE_BANKNAME = getText(e.element("CORRELATE_BANKNAME"));//附加行名
//	    	String CORRELATE_BANKCODE = getText(e.element("CORRELATE_BANKCODE"));//附加行号
//	    	String BUSINESSTYPE = getText(e.element("BUSINESSTYPE"));//业务类型
//	    	String ATTACHINFO = getText(e.element("ATTACHINFO"));//jyrq+cpzxh+znxh
	    	DFContentResult dfcr = new DFContentResult();
	    	dfcr.setUserName(CORRELATE_NAME);//用户名
	    	dfcr.setBankCard(CORRELATE_ACCTID);//卡号
	    	dfcr.setOpeningBank(CORRELATE_BANKNAME);//开户行
	    	dfcr.setTransDate(DTACCT);//日期
	    	dfcr.setTransMoney(Double.parseDouble(TRNAMT));//交易金额
	    	dfcr.setStatus(1);
	    	String key = CORRELATE_NAME+CORRELATE_ACCTID+TRNAMT;
	    	resultMap.put(key, dfcr);
  	    }
		return result;
	}
	
	private String getText(Element e){
		return null == e ? "" : e.getText();
	}
	
    
	@SuppressWarnings("unchecked")
	public String getNodeList(String xpath,String childPaths,String xpath2,String child2Paths)
	{
    	String result="";
		List<Node> list = xmlDoc.selectNodes(xpath);
		String[] childPath=childPaths.split("\\|");
		int c = 0;
		int f = 0;
	    for(Iterator i = list.iterator(); i.hasNext(); )
	    {
  	        Element e = (Element) i.next();
  	      if(c != 0) result+="|";
			for(int j=0;j<childPath.length;j++)
			{
				String[] child= childPath[j].split("\\,");
				String nodeText=e.element(child[1]).getText();
				System.out.println(child[0]+":"+nodeText);
				if(j != 0)result+=",";
				result+= nodeText;
  	        }
			if(!"".equals(xpath2))
			{
				result+="#";
				List<Node> list2=e.selectNodes(xpath2);
				String[] childPath2=child2Paths.split("\\|");
				f = 0;
				for(Iterator m = list2.iterator(); m.hasNext(); ){
		  	        Element e2 = (Element) m.next();
		  	        if(f != 0)result+="#";
		  	        for(int n=0;n<childPath2.length;n++){
		  	        	String[] child2= childPath2[n].split("\\,");
		  	        	String nodeText2=e2.element(child2[1]).getText();
		  	        	System.out.println(child2[0]+":"+nodeText2);
		  	        	if(n != 0)result+=",";
		  	        	result+= nodeText2;
		  	        }
		  	        f++;
			    }
			}
			System.out.println("");
			c++;
  	    }
		return result;
	}
	
	/**
	 * 循环获取一个节点集合的值
	 * @param xpath
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getNodeList(String xpath){
		List<Node> list = xmlDoc.selectNodes(xpath);
		List<String> result = new ArrayList<String>();
		for(Iterator i = list.iterator(); i.hasNext(); ){
  	        Element e = (Element) i.next();
  	        result.add(e.getText());
		}
		return result;
	}
}
